<?php

$test_series = $_GET['test_series'];

include 'db_connection.php';
include 'navigation.php';

$query = "select * from BATTERY_LOG where BATTERY_LOG.Series_id=$test_series order by BATTERY_LOG.Type, BATTERY_LOG.Time;";
$QueryResult = @mysqli_query($DBConnect, $query) or die("<p>Unable to execute query.</p>");	

$num_rows = mysqli_num_rows($QueryResult);
$num_attributes = mysqli_num_fields($QueryResult);
$attributes = mysqli_fetch_fields($QueryResult);

$query2 = "select * from TEST where Series_id=".$test_series." order by TEST.Type";
$QueryResult2 = @mysqli_query($DBConnect, $query2) or die("<p>Unable to execute query2.</p>");	
$num_tests = mysqli_num_rows($QueryResult2);
 
$query3 = "select MAX(Time) from BATTERY_LOG,TEST where TEST.Series_id=".$test_series." and BATTERY_LOG.Series_id=TEST.Series_id and BATTERY_LOG.Type=TEST.Type";
$QueryResult3 = @mysqli_query($DBConnect, $query3) or die("<p>Unable to execute query3.</p>");	
$row_w_max_time = mysqli_fetch_array($QueryResult3);
$max_time = $row_w_max_time['MAX(Time)'];
$num_x_axis_marks = ($max_time/5)+1; 
$num = mysqli_num_rows($QueryResult3);

$query4 = "select (COUNT(Charge) * SUM(Time*Charge) - SUM(Time)*SUM(Charge)) / (COUNT(Charge)*SUM(Time*Time) - SUM(Time)*SUM(Time)) from BATTERY_LOG where BATTERY_LOG.Series_id=$test_series group by BATTERY_LOG.Series_id, BATTERY_LOG.Type order by BATTERY_LOG.Type;";
$QueryResult4 = @mysqli_query($DBConnect, $query4) or die("<p>Unable to execute query.</p>");	

$query5 = "select (COUNT(Charge) * SUM(Time*Charge) - SUM(Time)*SUM(Charge)) / (COUNT(Charge)*SUM(Time*Time) - SUM(Time)*SUM(Time)) from BATTERY_LOG where BATTERY_LOG.Series_id=$test_series and BATTERY_LOG.Time < 35 group by BATTERY_LOG.Series_id, BATTERY_LOG.Type order by BATTERY_LOG.Type;";
$QueryResult5 = @mysqli_query($DBConnect, $query5) or die("<p>Unable to execute query.</p>");

?>

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();

     
        data.addColumn('string', 'Time');
        
	<?php
	$x=0;
	while ($row = mysqli_fetch_assoc($QueryResult2)) {
	$test_types[$x] = $row['Type'];
        ?>
	data.addColumn('number', '<?php echo $test_types[$x];?>');	
	<?php
	$x++;
        }
	?>
                
        data.addRows(<?php echo $num_x_axis_marks;?>);

	<?php
	$x_axis_value = 0;
	for($i=0; $i<$num_x_axis_marks; $i++){
	?>
	data.setValue(<?php echo $i;?>, 0, '<?php echo $x_axis_value;?>');
	<?php
	$x_axis_value += 5; 
	}
	?>

        <?php
	$x_axis_value = 0;
	$test_count = 0;
	while ($row = mysqli_fetch_assoc($QueryResult)) {         
	$current_test = $row['Type'];
	if($previous_test != $current_test){
		$test_count++;
		$x_axis_value = 0;
	}
	?>
	data.setValue(<?php echo $x_axis_value; ?>, <?php echo $test_count;?>, <?php echo $row['Charge'];?>);	
	<?php
	$x_axis_value++;
	$previous_test = $row['Type']; 
	}
	?>

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 800, height: 540, title: 'Time vs. Battery Charge', hAxis: {title: 'Time(Minutes)'}, vAxis: {title: 'Battery Charge(mA)'}});
      }
    </script>
  </head>

  <body>


<table>
<tr>
<td>

    <div align="center" id="chart_div"></div>

</td>
<td> 

	<div align="" id="avg_slope_stats">
   	 <table border=0>
	<tr bgcolor='#64AAD0'><td>Average slope (full battery to battery out)</td></tr>

	<?php
	for($i=0; $i< count($test_types);$i++){
	$row = mysqli_fetch_row($QueryResult4);
	$avg_slope_per_test_full[$i] = $row[0];
	echo "<tr><td>".$test_types[$i].": ".$avg_slope_per_test_full[$i]."</td></tr>";	
        }
	?>
   	 </table>
	</div>


	


	<div align="" id="avg_slope_test_constants">
   	<table border=0>
	<tr bgcolor='#64AAD0'><td>Subsystem Constants</td></tr>

	<?php
	for($i=0; $i< count($test_types);$i++){
	if($test_types[$i] == 'Baseline'){
		$baseline_slope_full = $avg_slope_per_test_full[$i];
	}
	}
    
	for($i=0; $i< count($test_types);$i++){
	if($test_types[$i] != 'Baseline'){
		$test_constant = $avg_slope_per_test_full[$i]/$baseline_slope_full;
		echo "<tr><td>".$test_types[$i].": ".$test_constant."</td></tr>";	
	}
	}
	?>
   	 </table>
	</div>


	


	<div align="" id="avg_slope_stats_60">
   	 <table border=0>
	<tr bgcolor='#64AAD0'><td>Average slope (0 to 30 mintues)</td></tr>

	<?php
	for($i=0; $i< count($test_types);$i++){
	$row = mysqli_fetch_row($QueryResult5);
	echo "<tr><td>".$test_types[$i].": ".$row[0]."</td></tr>";	
        }
	?>
   	 </table>
	</div>

</td>	
</tr>	
</table>


  </body>
</html>
